<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            @input="search"
            clearable
            v-model="queryinfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getuserlist(queryinfo.query)"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>

          <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
            <span>
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-form-item label="用户名" prop="username">
                  <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password">
                  <el-input v-model="ruleForm.password"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="ruleForm.email"></el-input>
                </el-form-item>

                <el-form-item label="手机号" prop="mobile">
                  <el-input v-model="ruleForm.mobile"></el-input>
                </el-form-item>
              </el-form>
            </span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="add">确 定</el-button>
            </span>
          </el-dialog>
        </el-col>
      </el-row>

      <el-table  class="table" :data="userlist" border="">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="setchange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column width="230px" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row)"
            ></el-button>
            <!-- 编辑 -->
            <el-dialog title="提示" :visible.sync="dialogVisiblebj" width="50%">
              <span>
                <el-form :model="ruleFormbj" :rules="rulesbj" ref="ruleFormbj">
                  <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleFormbj.username"></el-input>
                  </el-form-item>

                  <el-form-item label="邮箱" prop="email">
                    <el-input v-model="ruleFormbj.email"></el-input>
                  </el-form-item>

                  <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="ruleFormbj.mobile"></el-input>
                  </el-form-item>
                </el-form>
              </span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisiblebj = false">取 消</el-button>
                <el-button type="primary" @click="bj(scope.row)"
                  >确 定</el-button
                >
              </span>
            </el-dialog>

            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>

            <!-- 分配角色 -->
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-s-tools"
              @click="rolehh(scope.row)"
            ></el-button>

            <el-dialog
              title="分配角色"
              :visible.sync="dialogVisiblerole"
              width="40%"
            >
              <span>
                <p>当前用户: <span>{{role.username}}</span></p>
                <p>当前用户: <span>{{role.role_name}}</span></p>
                <p>
                  活动区域:
                  <el-select v-model="roleID" placeholder="请选择">
                    <el-option
                      v-for="item in roleList"
                      :key="item.id"
                      :label="item.roleName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </p>
              </span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisiblerole = false">取 消</el-button>
                <el-button type="primary" @click="fenuser"
                  >确 定</el-button
                >
              </span>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { userlistApi } from "@/http/api";
import { addone } from "@/http/api";
import { delone, bjone, setone ,roles,rolefeng} from "@/http/api";

import _ from "lodash";
export default {
  name: "users",
  data() {
    return {
      // 添加用户规则
      roleID:"",
      userID:'',
      roleList:[],
      role:{},
      rules: {
        username: [
          { required: true, message: "请输入用户名", triggle: "blur" },
          { min: 3, max: 10, message: "用户名在3-10位之间", triggle: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", triggle: "blur" },
          { min: 3, max: 10, message: "密码在3-10位之间", triggle: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入电话号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入正确电话号", trigger: "blur" },
        ],
      },
      rulesbj: {
        username: [
          { required: true, message: "请输入用户名", triggle: "blur" },
          { min: 3, max: 10, message: "用户名在3-10位之间", triggle: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入电话号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入正确电话号", trigger: "blur" },
        ],
      },

      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      userlist: [],
      total: 0,
      dialogVisible: false,
      dialogVisiblebj: false,
      dialogVisiblerole: false,
      ruleForm: {
        id: null,
        email: "",
        mobile: "",
      },
      editid: 0,
      ruleFormbj: {
        username: "",
        email: "",
        mobile: "",
      },

      value: false,
    
    };
  },
  created() {
    this.getuserlist();
  },
  methods: {
    async rolehh(row) {
      console.log(row);
      this.userID=row.id
      this.dialogVisiblerole = true;
      const {username,role_name}=row
      this.role={username,role_name}
      // console.log(this.role)
      const res=await roles()
      this.roleList=res
      console.log('角色',res)
      
    },
     async fenuser(){
        const res=await rolefeng(this.userID,this.roleID)
        this.getuserlist()
        console.log(res)
         this.dialogVisiblerole = false;
    },
    // 改变用户状态
    async setchange(row) {
      console.log(row, "zt");
      const res = await setone(row.id, row.mg_state);
    },
    edit(row) {
      console.log(row);
      this.dialogVisiblebj = true;
      this.editid = row.id;
      this.ruleFormbj.email = row.email;
      this.ruleFormbj.mobile = row.mobile;
      this.ruleFormbj.username = row.username;
    },
    // 编辑
    bj() {
      this.$refs.ruleFormbj.validate(async (valid) => {
        if (valid) {
          let res = await bjone(this.editid, this.ruleFormbj);
          console.log("ewqe", res);
          this.getuserlist();
          this.dialogVisiblebj = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 添加
    add() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const res = await addone(this.ruleForm);
          this.getuserlist();
          this.dialogVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 搜索lo
    search: _.debounce(function () {
      this.getuserlist();
    }, 300),

    //  删除
    async del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delone(row).then((res) => {
            this.getuserlist();
          });
          console.log("删除", row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryinfo.pagesize = val;
      this.getuserlist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryinfo.pagenum = val;
      this.getuserlist();
    },
    async getuserlist() {
      const res = await userlistApi(this.queryinfo);
      console.log("用户列表:", res);
      this.userlist = res.users;
      this.total = res.total;
    },
  },
};
</script>

<style lang="scss" scoped>
table{
  margin-top: 20px;
}
.el-breadcrumb {
  margin-top: 20px;
}
.el-row {
  margin-top: 25px;
}

.el-card {
  margin-top: 30px;
}
</style>
